<template>
	<div>
		<!--		按钮-->
		<el-row>
			<el-col :span="24">
				<el-button type="primary" :icon="Edit" @click="btn('新增', {})">新增</el-button
				>
			</el-col>
		</el-row>
		<!--		表单-->
		<el-row>
			<el-col :span="24">
				<el-table :data="state.itemList" stripe style="width: 100%">
					<el-table-column align="center" prop="id" label="编号" />
					<el-table-column align="center" prop="name" label="名称" />

					<el-table-column align="center" label="图片">
						<template #default="scope">
							<el-image
								style="width: 80px; height: 80px"
								:src="scope.row.path"
							/>
						</template>
					</el-table-column>
					<el-table-column align="center" prop="sort" label="排行" />
					<el-table-column
						align="center"
						fixed="right"
						label="操作"
						min-width="120"
					>
						<template #default="scope">
							<el-button
								link
								type="primary"
								size="small"
								@click.prevent="btn('修改', scope.row)"
							>修改</el-button
							>
							<el-button
								link
								type="primary"
								size="small"
								@click.prevent="del(scope.row.id)"
							>删除</el-button
							>
						</template>
					</el-table-column>
				</el-table>
			</el-col>
		</el-row>
	</div>
	<el-dialog
		v-model="state.dialogVisible"
		:title="state.title"
		width="50%"
		:close-on-click-modal="false"
		:close-on-press-escape="false"
	>
		<el-form
			ref="ruleFormRef"
			:rules="rules"
			:model="state.from"
			label-width="auto"
			style="max-width: 800px"
		>
			<el-form-item label="名称" prop="name">
				<el-input v-model="state.from.name" />
			</el-form-item>
			<el-form-item label="图片" prop="name">
				<el-upload class="avatar-uploader" action="http://ordinary.jimostudio.link:26451/api/upload?type=10&projectName=chinaAnimation" :show-file-list="false" :on-success="handleAvatarSuccess">
					<img v-if="state.from.path" :src="state.from.path" class="avatar"/>
					<el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
				</el-upload>
			</el-form-item>

			<el-form-item label="排行" prop="sort">
				<el-input v-model="state.from.sort" />
			</el-form-item>
		</el-form>
		<template #footer>
			<div class="dialog-footer">
				<el-button v-if="state.title === '修改'" @click="update(ruleFormRef)">确定 修改</el-button>
				<el-button v-else @click="save(ruleFormRef)">确定 新增</el-button>
			</div>
		</template>
	</el-dialog>
</template>
<script setup lang="ts">
import { Edit } from '@element-plus/icons-vue'
import type { UploadProps } from 'element-plus'
import { ref } from 'vue'
const ruleFormRef = ref<FormInstance>()
import type { FormInstance } from 'element-plus'
const rules = reactive({
	name: [
		{
			required: true,
			message: '名称不能为空',
			trigger: 'change',
		},
	],
	path: [
		{
			required: true,
			message: '路径不能为空',
			trigger: 'change',
		},
	],
	sort: [
		{
			required: true,
			message: '排行不能为空',
			trigger: 'change',
		},
	],

})
const state = reactive(<any>{
	imgPath: '',
	dialogVisible: false,
	title: '',
	//条件查询
	query: {
		province: '',
		name: '',
		page: 1,
		pagesize: 10,
		total: 10000,
	},
	itemList: [],
	from: {},
})
/**
 * 文件上传
 * @param response
 * @param uploadFile
 */
const handleAvatarSuccess: UploadProps['onSuccess'] = (response) => {
	state.from.path = response.data.path
}
function init() {

	http.get(`/carousel`).then((res) => {

			state.itemList = res.data
		})
}

//预打开按钮
function btn(type: string, data: any) {
	if (state.itemList.length >= 5) {
		toast.warning("轮播图最多添加五个！")
	}

	state.dialogVisible = true
	state.title = type
	if (type === '修改') {
		state.from = data
	}
}
//新增
const save = async (formEl: FormInstance | undefined) => {
	if (!formEl) return
	await formEl.validate((valid) => {
		if (valid) {
			http.post('carousel', state.from).then(() => {
				toast.success('新增成功')
				state.dialogVisible = false
				init()
			})
		}
	})
}
//修改
const update = async (formEl: FormInstance | undefined) => {
	if (!formEl) return
	await formEl.validate((valid) => {
		if (valid) {
			state.from.companyInfo = JSON.stringify(state.from.companyInfo)
			state.from.jobInfo = JSON.stringify(state.from.jobInfo)
			http.put('carousel', state.from).then(() => {
				toast.success('修改成功')
				state.dialogVisible = false
				state.from = {}
				init()
			})
		}
	})
}
//删除
function del(id: number) {
	http.delete(`/carousel/${id}`).then(() => {
		toast.success('删除成功')
		init()
	})
}

init()
</script>

<style scoped>
.page {
	margin: 20px 0;
	display: flex;
	justify-content: center;
	text-align: center;
}
::v-deep .avatar-uploader .el-upload {
	border: 1px dashed var(--el-border-color);
	border-radius: 6px;
	cursor: pointer;
	position: relative;
	overflow: hidden;
	transition: var(--el-transition-duration-fast);
}
::v-deep .avatar-uploader .el-upload:hover {
	border-color: var(--el-color-primary);
}
::v-deep .el-icon.avatar-uploader-icon {
	font-size: 28px;
	color: #8c939d;
	width: 178px;
	height: 178px;
	text-align: center;
}
.avatar {
	width: 178px;
	height: 178px;
}
</style>
<route lang="json">
{
"meta": {
"title": "后台",
"layout": "admin"
}
}
</route>
